package cn.dakaqi.sys.topic.services;

import cn.dakaqi.entities.SysTopic;
import cn.dakaqi.entities.SysTopicAgreen;
import cn.dakaqi.member.service.VolunteerService;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/6/3.
 */
@Component(value = "sysTopicAgreenService")
@Transactional
public class SysTopicAgreenServiceImpl implements SysTopicAgreenService
{
    @Autowired
    SysTopicAgreenDao sysTopicAgreenDao;
    @Autowired
    VolunteerService volunteerService;
    @Autowired
    SysTopicService sysTopicService;
    @Override
    public void create(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);
        String code = jsonObject.getString("code");
        String memberCode = jsonObject.getString("memberCode");
        int type = jsonObject.getIntValue("type");
        //检查当前用户是否已赞过当前话题
        SysTopicAgreen sysTopicAgreen = findLastComment(memberCode,code);
        if(null != sysTopicAgreen)
            throw new ServiceRuntimeException("您已赞过本话题,不可以重复操作");

        sysTopicAgreen = new SysTopicAgreen();
        sysTopicAgreen.setCreateTime(Clock.DEFAULT.getCurrentDate());
        sysTopicAgreen.setType(type);
        sysTopicAgreen.setSysTopic(sysTopicService.findByCode(code));
        sysTopicAgreen.setVolunteer(volunteerService.findByMemberCode(memberCode));
        this.save(sysTopicAgreen);
    }

    @Override
    public void save(SysTopicAgreen sysTopicAgreen) throws ServiceRuntimeException
    {
        sysTopicAgreenDao.save(sysTopicAgreen);
        SysTopic sysTopic = sysTopicAgreen.getSysTopic();
        sysTopic.setGoods(sysTopic.getGoods()+1);
        sysTopic.setParticipants(sysTopic.getParticipants()+1);
        sysTopicService.update(sysTopic);
    }

    @Override
    public Page<SysTopicAgreen> findBySysTopic(String code, int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);

        Map<String,Object> map = new HashMap<String, Object>();
        //map.put("LIKE_NAME",name);
        //Map<String, SearchFilter> filters = SearchFilter.parse(map);

        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("sysTopic.code", new SearchFilter("sysTopic.code", SearchFilter.Operator.EQ, code));
        Specification<SysTopicAgreen> spec = DynamicSpecifications.bySearchFilter(filters.values(), SysTopicAgreen.class);
        return this.sysTopicAgreenDao.findAll(spec,pageRequest);
    }

    @Override
    public SysTopicAgreen findLastComment(String memberCode, String code) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteer.memberCode", new SearchFilter("volunteer.memberCode", SearchFilter.Operator.EQ, memberCode));
        filters.put("sysTopic.code", new SearchFilter("sysTopic.code", SearchFilter.Operator.EQ, code));
        Specification<SysTopicAgreen> spec = DynamicSpecifications.bySearchFilter(filters.values(), SysTopicAgreen.class);
        List<SysTopicAgreen> data = this.sysTopicAgreenDao.findAll(spec);
        if(null != data && data.size()>0)
            return data.get(0);
        return null;
    }
}
